package com.putong35.common.baseactfrag.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.putong35.common.R;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.IComponentStatable;
import com.putong35.common.baseactfrag.ILoadable;
import com.putong35.common.baseactfrag.LoadState;
import com.putong35.common.baseactfrag.NetUtils;
import com.putong35.common.baseactfrag.view.BottomLoadStateView;
import com.putong35.common.baseactfrag.view.CenterToolbar;
import com.putong35.common.utils.EasyToast;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * 1. Activity定义了许多的状态，用户可以通过set(state)来切换状态，每个状态对应一个View，用户还可以自定义这个View
 * <p/>
 * 每个状态对应一个UI 布局,用户可以自定义这些布局,通过setXX方法（e.g setEmptyView）
 *
 * 2. Fragment默认没有title bar,User可以自定义自己的title bar
 *
 * @author Vivi Peng
 * @version 1.0.0
 * @e-mail pengveivei@sina.cn
 * @QQ 897058316
 * @since 1979-01-01
 */
public abstract class BaseFragment extends StateFragment implements IComponentStatable, ILoadable {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setCustomToolbar(new CenterToolbar(getActivity()));
        getCustomToolbar().setVisibility(View.GONE);
        ButterKnife.bind(this, view);
        return view;

    }


    public void setTitle(CharSequence title) {
        getCustomToolbar().setTitle(title);
    }

    public void setTitle(int titleId) {
        getCustomToolbar().setTitle(titleId);
    }

    @Override
    public CenterToolbar getCustomToolbar() {
        return (CenterToolbar) super.getCustomToolbar();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
        MobclickAgent.onPause(getActivity());
    }

    public void showToast(String msg) {
        EasyToast.showToast(getActivity(), msg);

    }

    public void showToast(int strId) {
        EasyToast.showToast(getActivity(), strId);
    }


    /**
     * 设置刷新的UI是否可以取消
     *
     * @param cancelable
     */
    public void setRefreshingCancelable(final boolean cancelable) {
        getEmptyRefreshingView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (cancelable) {
                    return false;
                } else {
                    return true;
                }
            }
        });
    }

    protected <V extends View> V findView(int resId) {
        return (V) getDataView().findViewById(resId);
    }

    @Override
    public void startLoading() {
        if (NetUtils.getNetworkStatus(getActivity()) == NetUtils.NETWORK_STATE_IDLE) {
            setState(CompState.EMPTY_INVALID_NEWWORK);
            return;
        }

        setState(CompState.EMPTY_REFRESHING);
        onStartLoading();

    }

    @Override
    public void onStartLoading() {

    }

    @Override
    public void setAutoLoading(boolean auto) {

    }

    @Override
    public void setTriggerLoadItemCount(int itemCount) {

    }

    @Override
    public BottomLoadStateView createBottomLoadStateView(LayoutInflater inflater) {
        return null;
    }


    @Override
    public void changeLoadState(LoadState loadState) {

    }

    @Override
    public View createEmptyErrorView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.base_default_empty_error, null);
		view.findViewById(R.id.lytMain).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startLoading();
			}
		});
        view.findViewById(R.id.btnReload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
				startLoading();
            }
        });
        return view;
    }

    @Override
    public View createEmptyInvalidNetView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.base_default_empty_invalid_net, null);
        view.findViewById(R.id.lytMain).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startLoading();
            }
        });
        return view;
    }

    @Override
    public View createEmptyView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.base_default_empty, null);
        view.findViewById(R.id.lytMain).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startLoading();
            }
        });
        return view;
    }

    @Override
    public View createEmptyRefreshingView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.base_default_refreshing, null);
    }
}
